<template>
	<section>
		<!--工具条 - 高级查询框-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="名称" width="120" sortable>
			</el-table-column>
      <el-table-column prop="description" label="描述" width="120" sortable>
      </el-table-column>
			<el-table-column prop="parent.name" label="父类型" width="100" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条 - 批量删除&分页条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination
          layout="prev, pager, next"
          :current-page="query.currentPage"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total"
          style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="新增/修改" :visible.sync="productTypeFormVisible" :close-on-click-modal="false">
			<el-form :model="productTypeForm" label-width="80px" :rules="productTypeFormRules" ref="productTypeForm">
				<el-form-item label="名称" prop="name">
					<el-input v-model="productTypeForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="父类型">
          <el-select v-model="productTypeForm.parent" clearable value-key="id" placeholder="请选择">
            <el-option
                v-for="item in parentTypes"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="productTypeForm.description" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="productTypeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="productTypeFormLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				query:{
				  currentPage:1,  // 当前页
          pageSize:10,  // 每页显示条数
          keyword:""  // 关键字
        },
        pageInfo:{
				  total:0,
          rows:[]
        },
				listLoading: false,
				sels: [],//列表选中列
        
				productTypeFormVisible: false,//新增修改界面是否显示
				productTypeFormLoading: false,
				productTypeFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					]
				},
        parentTypes:[],
				//新增修改界面数据
				productTypeForm: {
					id:null,
          name:"",
          parent:{
            id:null,
            name:""
          },
          description:""
				}
			}
		},
		methods: {
      search(){
        this.query.currentPage = 1;
        this.getProductTypes();
      },
			handleCurrentChange(val) {  // val是当前页
				this.query.currentPage = val;
				this.getProductTypes();
			},
			//获取产品类型列表
			getProductTypes() {
				this.listLoading = true;  // 弹出加载框
				this.$http.post("/productType",this.query)   // axios.defaults.baseURL+/productType  http://localhost:80/api/productType
                  .then(result => {
                    result = result.data; // result.data  {success:true,message:"",result:{total:1,rows:[]}}
                    console.log(result)
                    if(result.success){
                       this.pageInfo = result.result;
                       this.listLoading = false;  // 关闭加载框
                    }else{
                      this.$message({message: '查询分页失败!'+result.message, type: 'error'});
                    }
                  })
                  .catch(result => {
                    this.$message({message: "查询分页失败!", type: 'error'});
                  })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/productType/"+row.id)
                    .then(result => {
                       result = result.data;
                       if(result.success){
                         this.$message({message: '删除成功', type: 'success'});
                         // 刷新列表
                         this.query.currentPage = 1;
                         this.getProductTypes();
                         this.listLoading = false;  // 关闭弹出框
                       }else{
                         this.$message({message: '删除失败!'+result.message, type: 'error'});
                       }
                    })
				}).catch(() => {
          this.$message({message: '已取消删除', type: 'info'});
				});
			},

      //
      getParentTypes(){
         this.$http.get("/productType")
              .then(result => {
                  result = result.data;
                  if(result.success){
                      this.parentTypes = result.result;
                  }
              })
      },

			//显示编辑界面
			handleEdit: function (index, row) {
        console.log(row)
				this.productTypeFormVisible = true;
				this.productTypeForm = Object.assign({}, row);
				this.getParentTypes();
			},
			//显示新增界面
			handleAdd: function () {
				this.productTypeFormVisible = true;
				this.productTypeForm = {
          id:null,
          name:"",
          parent:{
            id:null,
            name:""
          },
          description:""
        }
        this.getParentTypes();
			},
			//新增修改保存方法
			save: function () {
				this.$refs.productTypeForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.productTypeFormLoading = true;
							let para = Object.assign({}, this.productTypeForm);
							this.$http.put("/productType",para)
                  .then(result => {
                      result = result.data;
                      if(result.success){
                        this.$message({message: '保存成功', type: 'success'});
                        this.productTypeFormLoading = false;
                        this.query.currentPage = 1;
                        this.getProductTypes();
                        this.productTypeFormVisible = false;
                      }else{
                        this.$message({message: '保存失败!'+result.message, type: 'error'});
                      }
                  }).catch(result => {
                      this.$message({message: '保存失败!', type: 'error'});
                  })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getProductTypes();
					});
				}).catch(() => {

				});
			}
		},
		mounted() { 
			this.getProductTypes();  // 做分页查询
		}
	}

</script>

<style scoped>

</style>